package core;

import constant.Constant;
import util.ImageUtil;

import java.awt.*;
import java.util.Random;

public class Food extends Object {


    public Food() {
        this.live = true;
        this.image = ImageUtil.images.get(String.valueOf(new Random().nextInt(10)));
        this.width = image.getWidth(null);
        this.height = image.getHeight(null);

        this.x = (int)(Math.random()*(Constant.GAME_WIDTH-width+10));
        this.y = (int)(Math.random()*(Constant.GAME_HEIGHT-height-40));
    }

    public void eaten(MySnake mySnake){
        if (mySnake.getRectangle().intersects(this.getRectangle())&&live&&mySnake.live){
            this.live = false;
            mySnake.setLength(mySnake.getLength()+1);
            mySnake.score += 2;
        }
    }

    @Override
    public void draw(Graphics g) {
        g.drawImage(image,x,y,null);
    }
}
